import { defineStore } from 'pinia';
import { store } from '@/store';
import { IParam } from '@/core';

interface AppState {
	/**
	 * 页面描述集合
	 *
	 * @type {any[]}
	 * @memberof AppState
	 */
	pageMetas: any[];
	/**
	 * 历史路径集合
	 *
	 * @type {string[]}
	 * @memberof AppState
	 */
	historyPathList: string[];
	/**
	 * 应用数据
	 *
	 * @type {*}
	 * @memberof AppState
	 */
	appData: any;
	/**
	 * 本地应用数据
	 *
	 * @type {*}
	 * @memberof AppState
	 */
	localData: any;
	/**
	 * 组织数据集合
	 *
	 * @type {IParam}
	 * @memberof AppState
	 */
	orgDataMap: Map<string, IParam[]>;
	/**
	 * 部门数据集合
	 *
	 * @type {IParam}
	 * @memberof AppState
	 */
	depDataMap: Map<string, IParam[]>;
	/**
	 * 部门人员数据集合
	 *
	 * @type {IParam}
	 * @memberof AppState
	 */
	deptPersonDataMap: Map<string, IParam[]>;
}

const useAppStore = defineStore({
	id: 'app',
	state: (): AppState => ({
		pageMetas: [],
		historyPathList: [],
		appData: {},
		localData: {},
		orgDataMap: new Map(),
		depDataMap: new Map(),
		deptPersonDataMap: new Map(),
	}),
	getters: {
		getPageMetas(): any[] {
			return this.pageMetas;
		},
		getPageMeta(): any {
			return (fullPath: string) => {
				if (fullPath) {
					const index = this.pageMetas.findIndex((pageMeta: IParam) =>
						Object.is(pageMeta.fullPath, fullPath)
					);
					if (index >= 0) {
						return this.pageMetas[index];
					}
				}
			};
		},
		getAppData(): any {
			if (!this.appData) {
				this.appData = {};
			}
			const result: any = JSON.parse(JSON.stringify(this.appData));
			const copyContext: any = result.context ? result.context : {};
			if (this.localData && Object.keys(this.localData).length > 0) {
				Object.assign(copyContext, this.localData);
			} else if (localStorage.getItem('localData')) {
				try {
					Object.assign(
						copyContext,
						JSON.parse(localStorage.getItem('localData') as string)
					);
				} catch (error) {
					console.warn(error);
				}
			}
			// Object.assign(result, { context: copyContext });
			return result;
		},
		/**
		 * 获取组织数据
		 *
		 * @return {*}
		 */
		getOrgData() {
			return (srfkey: string): IParam[] | undefined => {
				return this.orgDataMap.get(srfkey);
			};
		},
		/**
		 * 获取部门数据
		 *
		 * @return {*}
		 */
		getDepData() {
			return (srfkey: string): IParam[] | undefined => {
				return this.depDataMap.get(srfkey);
			};
		},
		/**
		 * 获取部门人员数据
		 *
		 * @return {*}
		 */
		getDeptPersonData() {
			return (srfkey: string): IParam[] | undefined => {
				return this.deptPersonDataMap.get(srfkey);
			};
		},
	},
	actions: {
		addAppData(appData: any) {
			this.appData = appData;
		},
		addOrgData(srfkey: string, orgData: IParam[]) {
			this.orgDataMap.set(srfkey, orgData);
		},
		addDepData(srfkey: string, depData: IParam[]) {
			this.depDataMap.set(srfkey, depData);
		},
		addDeptPersonData(srfkey: string, deptPersonData: IParam[]) {
			this.deptPersonDataMap.set(srfkey, deptPersonData);
		},
		clearAppData() {
			this.appData = {};
		},
		addPage(page: any) {
			if (!page) {
				return;
			}
			const inputPageMeta = page.meta;
			if (inputPageMeta && Object.keys(inputPageMeta).length > 0) {
				if (inputPageMeta.startPage) {
					const appName = App.getAppConfig().appName;
					window.sessionStorage.setItem(appName, page.fullPath);
				} else {
					const pageMeta: any = {};
					Object.assign(pageMeta, inputPageMeta, {
						info: null,
						fullPath: page.fullPath,
					});
					const index = this.pageMetas.findIndex((pageMeta: any) =>
						Object.is(pageMeta.fullPath, page.fullPath)
					);
					if (index < 0) {
						this.pageMetas.push(pageMeta);
					} else {
						const index2 = this.historyPathList.indexOf(page.fullPath);
						if (index2 >= 0) {
							this.historyPathList.splice(index2, 1);
						}
					}
					this.historyPathList.push(page.fullPath);
				}
			}
		},
		addLocalData(localData: any) {
			Object.assign(this.localData, localData);
			localStorage.setItem('localData', JSON.stringify(this.localData));
		},
		setPage({
			fullPath,
			caption,
			info,
		}: {
			fullPath: string;
			caption: string;
			info?: string;
		}) {
			if (fullPath) {
				const index = this.pageMetas.findIndex((pageMeta: IParam) =>
					Object.is(pageMeta.fullPath, fullPath)
				);
				if (index >= 0) {
					this.pageMetas[index].caption = caption;
					this.pageMetas[index].info = info;
				}
			}
		},
		//  删除导航页面
		deletePage(fullPath: string) {
			let delPage: any = null;
			if (fullPath) {
				const index = this.pageMetas.findIndex((pageMeta: IParam) =>
					Object.is(pageMeta.fullPath, fullPath)
				);
				if (index >= 0) {
					delPage = this.pageMetas[index];
					this.pageMetas.splice(index, 1);
				}
			}
			const index = this.historyPathList.findIndex((path: any) =>
				Object.is(path, delPage?.fullPath)
			);
			if (index >= 0) {
				this.historyPathList.splice(index, 1);
			}
		},
		closeAllPage() {
			if (this.pageMetas.length > 0) {
				this.pageMetas = [];
				this.historyPathList = [];
			}
		},
		closeOtherPage() {
			if (this.historyPathList.length > 0) {
				const curPath = this.historyPathList[this.historyPathList.length - 1];
				const index = this.pageMetas.findIndex((pageMeta: IParam) =>
					Object.is(pageMeta.fullPath, curPath)
				);
				if (index >= 0) {
					const pageMeta = this.pageMetas[index];
					this.pageMetas.splice(0, this.pageMetas.length, pageMeta);
					this.historyPathList.splice(
						0,
						this.historyPathList.length,
						pageMeta.fullPath
					);
				}
			}
		},
	},
});

export function useAppStoreWithOut() {
	return useAppStore(store);
}
